package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.BookMapper;
import com.itheima.domain.Book;
import com.itheima.domain.Record;
import com.itheima.domain.User;
import com.itheima.entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.itheima.service.BookService;
import com.itheima.service.RecordService;

import java.text.SimpleDateFormat;
import java.util.Date;
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private RecordService recordservice;

    @Override
    public PageResult selectnewbooks(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<java.awt.print.Book> books = bookMapper.selectBooks();
        return new PageResult(books.getTotal(), books);
    }

    @Override
    public Book findByid(String id) {
        return bookMapper.findByid(id);
    }

    @Override
    public Integer borrowerbook(Book book) {
        Book byid = this.findByid(book.getId() + "");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        book.setBorrowTime(dateFormat.format(new Date()));//获取当前时间
        book.setStatus("1");
        book.setPrice(byid.getPrice());
        book.setUploadTime(byid.getUploadTime());
        return bookMapper.editBook(book);
    }

    @Override
    public PageResult search(Book book, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Book> page = bookMapper.searchbook(book);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Integer addbook(Book book) {
        new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        book.setUploadTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));//获取当前时间
        return bookMapper.addbook(book);
    }

    @Override
    public Integer editbook(Book book) {
        return bookMapper.editBook(book);
    }

    @Override
    public PageResult searchborrowed(Book book, User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Book> page;
        book.setBorrower(user.getName());
        if("ADMIN".equals(user.getRole())){
         page= bookMapper.selectborrowed(book);
        }else {
            page=bookMapper.selectmyborrowed(book);
        }
        return new PageResult(page.getTotal(), page.getResult());//返回分页结果
    }

    @Override
    public boolean returnbook(String id, User user) {
        Book book = this.findByid(id);
        boolean equals = book.getBorrower().equals(user.getName());
        if(equals){
           book.setStatus("2");
            bookMapper.editBook(book);
        }
        return equals;
    }

    @Override
    public Integer returnconfirm(String id) {
        Book book = this.findByid(id);
        Record recond = this.setrecond(book);
        book.setStatus("0");
        book.setBorrower("");
        book.setBorrowTime("");
        book.setReturnTime("");
        Integer count = bookMapper.editBook(book);
        if(count==1){
            return recordservice.addrecond(recond);
        }

        return 0;
    }

    private Record setrecond(Book book){
        Record recond = new Record();
        recond.setBookname(book.getName());
        recond.setBookisbn(book.getIsbn());
        recond.setBookborrower(book.getBorrower());
        recond.setBorrowTime(book.getBorrowTime());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        recond.setRemandTime(dateFormat.format(new Date()));
        return recond;
    }
}
